<template>
    <section class="jumbotron">
        <h3 class="jumbotron-heading">Search Github Users</h3>
        <div>
            <input type="text" placeholder="enter the name you search"  v-model="keyword"/>&nbsp;<button @click="getUsers">Search</button>
        </div>
    </section>
</template>

<script>
import pubsub from "pubsub-js";
import axios from 'axios';
export default {
    name: 'search',
    data(){
        return {
            keyword: null,
            info: {
                isFirst: false,
                userList: [],
                loading: false,
                errMsg: null
            }
        }
    },
    methods:{
        getUsers(){
            this.info = {
                userList:[], loading: true, errMsg: null
            }
            pubsub.publish('search', this.info);
            axios.get(`https://api.github2.com/search/users?q=${this.keyword}`)
                .then(
                    response=>{
                        console.log(response.data);
                        this.info = {
                            userList:response.data.items, loading: false, errMsg: null
                        }
                        pubsub.publish('search',this.info)
                    },
                    err=>{
                        this.info = {
                            userList:[], loading: false, errMsg: err.message
                        }
                        pubsub.publish('search', this.info);
                        console.log(err)

                    }
                )
        }
    }
}
</script>
<style scoped>

</style>
